package com.group.result;

import com.group.constants.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * com.gxa.pojo.vo
 * User: 恩哲欣
 * Date: 2024/10/18 18:23
 * Motto: Where there is a will, there is a way!
 * Description:
 * Version: V1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel("响应的结果集")
public class Result<T> {
    @ApiModelProperty("响应的状态码")
    private Integer code;
    @ApiModelProperty("提示信息")
    private String msg;
    @ApiModelProperty("总记录数")
    private Long count;
    @ApiModelProperty("响应的数据")
    private T data;
    // 构建分页结果
    public static <T> Result<T> buildSuccess(Long count, T data) {
        Result<T> result = new Result<>();
        result.setCode(Constants.SUCCESS_STATUS_CODE);
        result.setMsg(Constants.SUCCESS_STATUS_INFO);
        result.setCount(count);
        result.setData(data);
        return result;
    }

    // 构建单个结果的成功
    public static <T> Result<T> buildSuccess(T data) {
        return buildSuccess(null, data);
    }

    // 构建不带数据的成功
    public static <T> Result<T> buildSuccess() {
        return buildSuccess(null);
    }

    // 构建失败信息
    public static <T> Result<T> buildFail(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> buildFail(ResultEnum resultEnum) {
        Result<T> result = new Result<>();
        result.setCode(resultEnum.getCode());
        result.setMsg(resultEnum.getMessage());
        return result;
    }
}
